package com.junpuxin.mapper;

import com.junpuxin.dto.MenuPageDTO;
import com.junpuxin.entity.Menu;
import com.junpuxin.entity.RoleMenu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author junpuxin
 */
@Repository
public interface MenuMapper {

    @Insert("""
            insert into ums_menu (
            title, url, icon, pid, info, version, deleted, created, updated
            )
            values (
            #{title}, #{url}, #{icon}, #{pid}, #{info},
            #{version}, #{deleted}, #{created}, #{updated}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Menu menu);


    @Results(
            id = "menuResultMap",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "pid", column = "pid"),
                    @Result(property = "parent", column = "pid", one = @One(select = "com.wanghaoran.mapper.MenuMapper.select")),
            }
    )
    @Select("""
            select * from ums_menu t
            where t.id = #{param1} and t.deleted = 0
            """)
    Menu select(Long id);

    @ResultMap("menuResultMap")
    @Select("""
            <script>
            select * from ums_menu t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                <if test='pid != null'> pid = #{pid} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Menu> list(MenuPageDTO dto);

    @Update("""
            <script>
            update ums_menu
            <set>
               <if test = 'title != null'> title = #{title}, </if>
               <if test = 'url != null'> url = #{url}, </if>
               <if test = 'icon != null'> icon = #{icon}, </if>
               <if test = 'pid != null'> pid = #{pid}, </if>
               <if test = 'info != null'> info = #{info},</if>
               <if test = 'version != null'>version = #{version},</if>
               <if test = 'deleted != null'>deleted = #{deleted},</if>
               <if test = 'created != null'>created = #{created},</if>
               <if test = 'updated != null'>updated = #{updated},</if>
               version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Menu menu);


    @Update("""
            update ums_menu set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_menu set deleted = 1, updated = current_timestamp where
            <foreach collection="list" item="e" open="id in (" close=")" separator=",">
                ${e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);


    @Select("""
            select * from ums_menu t1 where id in (
                select t2.fk_menu_id from ums_role_menu t2
                where t2.fk_role_id in (
                    select t3.fk_role_id from ums_emp_role t3
                    where t3.fk_emp_id = #{param1} and t3.deleted = 0
                ) and t2.deleted = 0
            ) and t1.deleted = 0
            """)
    List<Menu> listByEmpId(Long empId);

    @ResultMap("menuResultMap")
    @Select("""
            select * from ums_menu t1 where t1.id in (
            select fk_menu_id from ums_role_menu t2 
            where t2.fk_role_id = #{param1} and t2.deleted = 0)
            and t1.deleted = 0
            """)
    List<Menu> listByRoleId(Long roleId);

    @Update("""
            update ums_role_menu set deleted = 1, updated = current_timestamp 
            where fk_role_id = #{param1};
            """)
    int deleteRoleMenuByRoleId(Long roleId);

    @Insert("""
            <script>
            insert into ums_role_menu
            (fk_role_id, fk_menu_id, version, deleted, created, updated)
            values 
            <foreach collection='list' item='e' separator=','>
                (#{e.fkRoleId}, #{e.fkMenuId}, #{e.version}, 
                #{e.deleted}, #{e.created}, #{e.updated})
            </foreach>
            </script>
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRoleMenuBatch(List<RoleMenu> roleMenus);

}
